import axios from "axios";
import useStore from '../store'

const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 80000,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    changeOrigin: true,  //允许跨域
});


// 在发送请求之前进行拦截
service.interceptors.request.use(
  config => {
    // 从 store 中获取 token
    const token = useStore().user.token;
    console.log(token)
    // 如果存在 token，就设置请求头
    if (token) {
      config.headers['Authorization'] = `bearer ${token}`;
    }

    return config;
  },
  error => {
    // 请求错误时的处理
    return Promise.reject(error);
  }
);

export default service;
